package cn.anxingongyou.anxin.controller;

import cn.anxingongyou.anxin.entity.EmployerBudget;
import cn.anxingongyou.anxin.service.EmployerBudgetService;
import cn.anxingongyou.anxin.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class EmployerBudgetController {

    private final EmployerBudgetService employerBudgetService;
    @Autowired
    public EmployerBudgetController(EmployerBudgetService employerBudgetService) {
        this.employerBudgetService = employerBudgetService;
    }

    @ApiOperation("添加雇主收支信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/employerbudget/add",method = RequestMethod.POST)
    public Map addEmployerBudget(
            @RequestBody @ApiParam(value = "雇主收支信息", required = true)EmployerBudget employerBudget
            ){
        employerBudget.setCreated(new Date());
        employerBudget.setUpdated(new Date());
        int res = employerBudgetService.insert(employerBudget);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("api添加雇主收支信息")
    @ApiResponses({
            @ApiResponse(code = 201,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @RequestMapping(value = "/api/employerbudget/add",method = RequestMethod.POST)
    public Map apiAddEmployerBudget(
            @RequestBody @ApiParam(value = "雇主收支信息", required = true)EmployerBudget employerBudget
    ){
        employerBudget.setCreated(new Date());
        employerBudget.setUpdated(new Date());
        int res = employerBudgetService.insert(employerBudget);
        if(res > 0) {
            return Result.instance(201,"添加成功").toJsonMap();
        } else {
            return Result.instance(500,"添加失败").toJsonMap();
        }
    }

    @ApiOperation("删除雇主收支信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "删除成功"),
            @ApiResponse(code = 500,message = "删除失败")
    })
    @RequestMapping(value = "/employerbudget/del/{id}",method = RequestMethod.GET)
    public Map delEmployerBudget(
            @PathVariable @ApiParam(value = "雇主收支主键", required = true) Long id
    ){
        int res = employerBudgetService.deleteByPrimaryKey(id);
        if(res > 0) {
            return Result.instance(204,"删除成功").toJsonMap();
        } else {
            return Result.instance(500,"删除失败").toJsonMap();
        }
    }

    @ApiOperation("更新雇主收支信息")
    @ApiResponses({
            @ApiResponse(code = 204,message = "更新成功"),
            @ApiResponse(code = 500,message = "更新失败")
    })
    @RequestMapping(value = "/employerbudget/update",method = RequestMethod.POST)
    public Map updateEmployerBudget(
            @RequestBody @ApiParam(value = "雇主收支信息,必传主键", required = true)EmployerBudget employerBudget
    ){
        int res = employerBudgetService.updateByPrimaryKeySelective(employerBudget);
        if(res > 0) {
            return Result.instance(204,"更新成功").toJsonMap();
        } else {
            return Result.instance(500,"更新失败").toJsonMap();
        }
    }

    @ApiOperation("获取雇主收支信息通过")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/employerbudget/findByWorkerBudget",method = RequestMethod.POST)
    public Map findOneEmployerBudget(
            @RequestBody @ApiParam(value = "雇主收支信息", required = true)EmployerBudget employerBudget
    ){
        EmployerBudget employerBudgetl = employerBudgetService.selectOne(employerBudget);
        if(employerBudgetl != null) {
            return Result.<EmployerBudget>instance(200,"获取成功",employerBudgetl).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取雇主收支信息通过主键")
    @ApiResponses({
            @ApiResponse(code = 200,message = "获取成功"),
            @ApiResponse(code = 500,message = "获取失败")
    })
    @RequestMapping(value = "/employerbudget/findOneById/{id}",method = RequestMethod.GET)
    public Map findOneById(
            @PathVariable @ApiParam(value = "雇主收支主键", required = true) Long id
    ){
        EmployerBudget employerBudgetl = employerBudgetService.selectOneByPrimaryKey(id);
        if(employerBudgetl != null) {
            return Result.<EmployerBudget>instance(200,"获取成功",employerBudgetl).toJsonMap();
        } else {
            return Result.instance(500,"获取失败").toJsonMap();
        }
    }

    @ApiOperation("获取所有雇主收支")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/employerbudget/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map selectByEmployerBudget(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "雇主收支信息", required = true)  EmployerBudget employerBudget){
        PageInfo<EmployerBudget> pageInfo = employerBudgetService.page(pageNum, pageSize, employerBudget);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

    @ApiOperation("api获取所有雇主收支")
    @ApiResponses({
            @ApiResponse(code = 200,message = "查询成功"),
            @ApiResponse(code = 500,message = "查询失败")
    })
    @RequestMapping(value = "/api/employerbudget/page/{pageNum}/{pageSize}",method = RequestMethod.POST)
    public Map apiSelectByEmployerBudget(
            @PathVariable("pageNum") @ApiParam(value = "第几页", required = true) int pageNum,
            @PathVariable("pageSize") @ApiParam(value = "每页多少条", required = true) int pageSize,
            @RequestBody @ApiParam(value = "雇主收支信息", required = true)  EmployerBudget employerBudget){
        PageInfo<EmployerBudget> pageInfo = employerBudgetService.page(pageNum, pageSize, employerBudget);
        Map<String,Object> res = new HashMap<>();
        res.put("total", pageInfo.getTotal());
        res.put("pageNum", pageInfo.getPageNum());
        res.put("pageSize", pageInfo.getPageSize());
        res.put("list",pageInfo.getList());
        return Result.<Map>instance(200,"查询成功",res).toJsonMap();
    }

}
